﻿using Crossroad.Core.Configs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Crossroad.Core
{
    public partial class DataUtil
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string ConnectionString => AppConfig.ConnectionString;

        /// <summary>
        /// 用来处理事务多表查询和复杂的操作
        /// </summary>
        protected SqlSugarClient db;

        public DataUtil()
        {
            db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectionString,
                DbType = AppConfig.DbType,
                //从特性读取主键和自增列信息
                InitKeyType = InitKeyType.Attribute,
                //开启自动释放模式和EF原理一样我就不多解释了
                IsAutoCloseConnection = true,
            });
            //调式代码 用来打印SQL 
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine($"{sql}\r\n{db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))}");
            };
        }



    }
}
